<?php

namespace app\api\controller;

use app\api\service\PackagedService;
use app\common\controller\ApiBase;
use think\Db;
use think\Exception;

class Packaged extends ApiBase
{

    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 列表数据
     * @return null
     */
    public function list()
    {
        $service = new PackagedService();
        $res = $service->getPackagedList($this->getPaginate());

        $this->success('成功', $res);
    }

    public function detail()
    {
        $this->checkRequestParam(\app\common\validate\Packaged::class);
        $param = $this->request->param();

        $service = new PackagedService();
        $res = $service->getPackagedDetail($param['id']);
        if (empty($res)) {
            $this->error('找不到相关的套餐信息！');
        }
        $this->success('成功', $res);
    }

    public function submit()
    {
        $this->checkRequestParam(\app\common\validate\Packaged::class);
        $param = $this->request->param();

        $service = new PackagedService();
        $userId = $this->auth->id;
        Db::startTrans();
        try {
            $order = $service->submitOrder($param, $userId);
            if (empty($order)) {
                Db::rollback();
                $this->error($service->getMsg());
            }
            Db::commit();

            $this->success('成功', ['order' => $order]);
        }catch (Exception $e) {
            Db::rollback();
            $this->errorTrace('提交套餐订单失败。',$e);
        }

    }
}
